<?php

namespace app\common\model;

use think\Model;

/**
 * 微信小程序二维码
 */
class WxCode extends Model
{

    /**
     * 个人邀请二维码
     *
     * @param int $user_id 用户ID
     */
    public function getUserQrCode($user_id)
    {
        try {
            $user = model('app\common\model\User')->get($user_id);
            $invite_code = $user->invite_code;
            $qr_code = $user->qr_code;
            if(empty($qr_code)){
                $qr_code = (new \app\common\library\WxCode)->getUserCode($user_id,$invite_code);
            }
            $qr_code = full_url($qr_code);

            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => [
                    'qr_code' => $qr_code
                ]
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => $e->getLine()
            ];
        }
    }

    /**
     * 个人邀请海报图
     *
     * @param int $user_id 用户ID
     */
    public function getUserInvitePosterImage($user_id)
    {
        try {
            $user = model('app\common\model\User')->get($user_id);
            $invite_poster_image = $user->invite_poster_image;
            if(empty($invite_poster_image)){
                $invite_poster_image = (new \app\common\library\WxCode)->posterImage($user_id);
                $user->invite_poster_image = $invite_poster_image;
                $user->save();
            }
            $invite_poster_image = full_url($invite_poster_image);

            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => [
                    'invite_poster_image' => $invite_poster_image
                ]
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => $e->getLine()
            ];
        }
    }


}